<template>
    <el-dialog :title="!dataForm.pkDept ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <ec-form-item-header value="部门结构信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="所属集团" prop="pkGroup">
                        <ec-group-tree-select v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                        </ec-group-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属组织" prop="pkOrg">
                        <ec-org-tree-select :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg" placeholder="请选择所属组织">
                        </ec-org-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="上级部门" prop="pkFatherdept">
                        <ec-dept-tree-select :pkGroup="dataForm.pkGroup" :pkOrg="dataForm.pkOrg"
                            v-model="dataForm.pkFatherdept" placeholder="请选择上级部门">
                        </ec-dept-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                </el-col>
            </el-row>
            <ec-form-item-header value="部门信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="部门名称" prop="deptName">
                        <el-input v-model="dataForm.deptName" placeholder="部门名称" maxlength="30" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="简称" prop="shortName">
                        <el-input v-model="dataForm.shortName" placeholder="简称" maxlength="20" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="部门类型" prop="deptType">
                        <ec-dict-select v-model="dataForm.deptType" placeholder="部门类型" dictType="dept_type">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="内部编码" prop="innerCode">
                        <el-input v-model="dataForm.innerCode" placeholder="内部编码" maxlength="20" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="负责人" prop="principal">
                        <!-- <ec-select v-model="dataForm.principal" placeholder="负责人" source="business"></ec-select> -->
                        <ec-user-select v-model="dataForm.principal" :pkGroup="dataForm.pkGroup"
                            :pkOrg="dataForm.pkOrg" placeholder="负责人" @getOptionDetail="getPrincipal"></ec-user-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="负责岗位" prop="resposition">
                        <el-input v-model="dataForm.resposition" placeholder="负责岗位"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="电话" prop="tel">
                        <el-input v-model.number="dataForm.tel" placeholder="电话" maxlength="11" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12"></el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="备注" prop="memo">
                        <el-input v-model="dataForm.memo" placeholder="备注" maxlength="30" show-word-limit></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <ec-form-item-header value="数据信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="显示顺序" prop="displayOrder">
                        <el-input v-model="dataForm.displayOrder" placeholder="显示顺序"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="启用状态" prop="enableState">
                        <ec-dict-select v-model="dataForm.enableState" placeholder="启用状态" dictType="enable_state">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as deptService from '@/api/upms/dept'
    export default {
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            }
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkDept: 0,
                    pkFatherdept: '',
                    pkGroup: '',
                    pkOrg: '',
                    principal: '',
                    resposition: '',
                    deptName: '',
                    shortName: '',
                    tel: '',
                    deptType: '',
                    displayOrder: '',
                    enableState: 'Y',
                    innerCode: '',
                    memo: '',
                    address: '',
                    dataoriginflag: '',
                    def1: '',
                    def2: '',
                    def3: '',
                    def4: '',
                    def5: '',
                    revision: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    deptName: [{
                        required: true,
                        message: '部门名称不能为空',
                        trigger: 'change'
                    }],
                    deptType: [{
                        required: true,
                        message: '部门类型不能为空',
                        trigger: 'blur'
                    }],
                    enableState: [{
                        required: true,
                        message: '启用状态不能为空',
                        trigger: 'blur'
                    }],
                    tel: [{
                        required: true,
                        validator: checkPhone,
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkDept = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkDept) {
                        deptService.getdeptinfo(this.dataForm.pkDept).then(data => {
                            this.dataForm.pkFatherdept = data.pkFatherdept
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.principal = data.principal
                            this.dataForm.resposition = data.resposition
                            this.dataForm.deptName = data.deptName
                            this.dataForm.shortName = data.shortName
                            this.dataForm.tel = data.tel
                            this.dataForm.deptType = data.deptType
                            this.dataForm.displayOrder = data.displayOrder
                            this.dataForm.enableState = data.enableState
                            this.dataForm.innerCode = data.innerCode
                            this.dataForm.memo = data.memo
                            this.dataForm.address = data.address
                            this.dataForm.dataoriginflag = data.dataoriginflag
                            this.dataForm.def1 = data.def1
                            this.dataForm.def2 = data.def2
                            this.dataForm.def3 = data.def3
                            this.dataForm.def4 = data.def4
                            this.dataForm.def5 = data.def5
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkDept': this.dataForm.pkDept || undefined,
                            'pkFatherdept': this.dataForm.pkFatherdept,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'principal': this.dataForm.principal,
                            'resposition': this.dataForm.resposition,
                            'shortName': this.dataForm.shortName,
                            'deptName': this.dataForm.deptName,
                            'tel': this.dataForm.tel,
                            'deptType': this.dataForm.deptType,
                            'displayOrder': this.dataForm.displayOrder,
                            'enableState': this.dataForm.enableState,
                            'innerCode': this.dataForm.innerCode,
                            'memo': this.dataForm.memo,
                            'address': this.dataForm.address,
                            'dataoriginflag': this.dataForm.dataoriginflag,
                            'def1': this.dataForm.def1,
                            'def2': this.dataForm.def2,
                            'def3': this.dataForm.def3,
                            'def4': this.dataForm.def4,
                            'def5': this.dataForm.def5,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkDept ? deptService.adddept(params) : deptService
                            .updatedept(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
            // 自动获取手机号
            getPrincipal(info){
                if (info && this.$isEmpty(this.dataForm.tel)){
                    this.dataForm.tel = info.phone
                }
            }
        }
    }
</script>